package com.ltu.util.datetime;

import java.math.BigInteger;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateTimeUtil { 
	/**  
	   * 获取现在时间
	   * 
	   * @return 返回时间类型 yyyy-MM-dd HH:mm:ss
	 * @throws ParseException 
	   */
	public static String getNowDate(Date date) {
	  
	   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	   String dateStr = formatter.format(date);	  
	   return dateStr;
	}
	/**
	 * 根据出生日期计算年龄
	 * @param birthDay
	 * @return
	 */
	public static int getAge(Date birthDay) {
		Calendar cal = Calendar.getInstance();
		if (cal.before(birthDay)) { // 出生日期晚于当前时间，无法计算
			throw new IllegalArgumentException("The birthDay is before Now.It's unbelievable!");
		}
		int yearNow = cal.get(Calendar.YEAR); // 当前年份
		int monthNow = cal.get(Calendar.MONTH); // 当前月份
		int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH); // 当前日期
		cal.setTime(birthDay);
		int yearBirth = cal.get(Calendar.YEAR);
		int monthBirth = cal.get(Calendar.MONTH);
		int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
		int age = yearNow - yearBirth; // 计算整岁数
		if (monthNow <= monthBirth) {
			if (monthNow == monthBirth) {
				if (dayOfMonthNow < dayOfMonthBirth)
					age--;// 当前日期在生日之前，年龄减一
			} else {
				age--;// 当前月份在生日之前，年龄减一

			}
		}
		return age;
	}
	/**  
	   * 获取现在时间
	   * 
	   * @return 返回时间类型 yyyy-MM-dd HH:mm:ss
	   * @throws ParseException 
	   */
	public static String getNowDate(){
		Date date=new Date();
	   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	   String dateStr = formatter.format(date);	  
	   return dateStr;
	}
	/**  
	   *时间字符串转时间
	   * 
	   */
	public static Date strToDate(String dateStr){
	   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
	   Date date=null;
		try {
			date = formatter.parse(dateStr);
		} catch (ParseException e) {

			e.printStackTrace();
		} 
	   return date;
	}
	
	/** 
     * 时间戳转换成日期格式字符串 
     * @param seconds 精确到秒的字符串 
     * @param formatStr 
     * @return 
     */  
    public static String timeStamp2Date(String seconds,String format) {  
        if(seconds == null || seconds.isEmpty() || seconds.equals("null")){  
            return "";  
        }  
        if(format == null || format.isEmpty()) format = "yyyy-MM-dd HH:mm:ss";  
        SimpleDateFormat sdf = new SimpleDateFormat(format);  
        return sdf.format(new Date(Long.valueOf(seconds+"000")));  
    }  
    /** 
     * 时间戳转换成日期格式字符串 
     * @param seconds 精确到毫秒的字符串 
     * @param formatStr 
     * @return 
     */  
    public static String stamp2DateMS(String seconds,String format) {  
    	if(seconds == null || seconds.isEmpty() || seconds.equals("null")){  
    		return "";  
    	}  
    	if(format == null || format.isEmpty()) format = "yyyy-MM-dd HH:mm:ss.S";  
    	SimpleDateFormat sdf = new SimpleDateFormat(format);  
    	return sdf.format(new Date(Long.valueOf(seconds)));  
    }  
    /** 
     * 日期格式字符串转换成时间戳 
     * @param date 字符串日期 
     * @param format 如：yyyy-MM-dd HH:mm:ss 
     * @return 
     */  
    public static String date2TimeStamp(String date_str,String format){  
        try {  
            SimpleDateFormat sdf = new SimpleDateFormat(format);  
            return String.valueOf(sdf.parse(date_str).getTime()/1000);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return "";  
    }  
      
    /** 
     * 取得当前时间戳（精确到秒） 
     * @return 
     */  
    public static String getStampTime(){  
        long time = System.currentTimeMillis();  
        String t = String.valueOf(time/1000);  
        return t;  
    }  
      
    /** 
     * 取得当前时间戳（精确到秒） 
     * @return 
     */  
    public static Long getLongStampTime(){  
    	Long time = System.currentTimeMillis()/1000;  
    	
        return time;  
    }
    
    
    public static void main(String[] args) {
    	BigInteger time = BigInteger.valueOf( (System.currentTimeMillis()));  
    	System.out.println(time);
    }
	
}
